using UniversalAdminSystem.Domian.PermissionManagement.Aggregate;
using UniversalAdminSystem.Domian.PermissionManagement.Interfaces;

namespace UniversalAdminSystem.Domian.PermissionManagement.Services;

/// <summary>
/// 权限分配领域服务实现
/// 提供权限分配给角色的领域业务逻辑实现
/// </summary>
public class AssignPermissionDomainService : IAssignPermissionDomainService
{
    /// <summary>
    /// 为角色分配权限
    /// </summary>
    /// <param name="permission">权限实体</param>
    /// <param name="role">角色实体</param>
    /// <returns>分配是否成功</returns>
    public bool AssignPermission(Permission permission, Role role)
    {
        try
        {
            if (role == null)
                throw new InvalidOperationException("权限分配失败！该角色不存在");

            if (permission == null)
                throw new InvalidOperationException("权限分配失败！该权限不存在");
                
            role.AddPermission(permission);
            return true;
        }
        catch (ArgumentException)
        {
            throw;
        }
    }

    public bool RemovePermission(Permission permission, Role role)
    {
        try
        {
            if (role == null)
                throw new InvalidOperationException("权限移除失败！该角色不存在");
            if (permission == null)
                throw new InvalidOperationException("权限移除失败！该权限不存在");
            role.RemovePermission(permission);
            return true;
        }
        catch (ArgumentException)
        {
            throw;
        }
    }

    public bool AssignPermissions(IEnumerable<Permission> permissions, Role role)
    {
        try
        {
            if (role == null)
                throw new InvalidOperationException("权限分配失败！该角色不存在");
            if (permissions == null)
                throw new InvalidOperationException("权限分配失败！权限集合为空");
            role.AddPermissions(permissions);
            return true;
        }
        catch (ArgumentException)
        {
            throw;
        }
    }

    public bool RemovePermissions(IEnumerable<Permission> permissions, Role role)
    {
        try
        {
            if (role == null)
                throw new InvalidOperationException("权限移除失败！该角色不存在");
            if (permissions == null)
                throw new InvalidOperationException("权限移除失败！权限集合为空");
            role.RemovePermissions(permissions);
            return true;
        }
        catch (ArgumentException)
        {
            throw;
        }
    }
}